import axios from 'axios';
import { BASE_URL } from '../config';

// 创建 axios 实例
const axiosInstance = axios.create({
  baseURL: BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

const adminApi = {
  // 获取统计数据
  getStatistics: async (dateRange) => {
    const response = await axios.get(`${BASE_URL}/admin/statistics`, { params: { dateRange } });
    return response.data;
  },

  // 获取公告列表
  getAnnouncements: async () => {
    const response = await axios.get(`${BASE_URL}/admin/announcements`);
    return response.data;
  },

  // 创建公告
  createAnnouncement: async (data) => {
    const response = await axios.post(`${BASE_URL}/admin/announcements`, data);
    return response.data;
  },

  // 获取用户统计
  getUserStats: async (params) => {
    const response = await axios.get(`${BASE_URL}/admin/users/stats`, { params });
    return response.data;
  },

  // 获取课程统计
  getCourseStats: async (params) => {
    const response = await axios.get(`${BASE_URL}/admin/courses/stats`, { params });
    return response.data;
  },

  getDashboardData: async (timeRange) => {
    const response = await axios.get(`${BASE_URL}/admin/dashboard`, {
      params: { timeRange }
    });
    return response.data;
  },

  updateAnnouncement: async (announcement) => {
    const response = await axios.post(`${BASE_URL}/admin/announcements`, announcement);
    return response.data;
  },

  // 用户管理
  getUsers: async () => {
    const response = await axiosInstance.get('/users');
    return response.data;
  },

  updateUser: async (userId, userData) => {
    const response = await axiosInstance.put(`/users/${userId}`, userData);
    return response.data;
  },

  deleteUser: async (userId) => {
    const response = await axiosInstance.delete(`/users/${userId}`);
    return response.data;
  },

  // 课程管理
  getCourses: async () => {
    const response = await axiosInstance.get('/courses');
    return response.data;
  },

  createCourse: async (courseData) => {
    const response = await axiosInstance.post('/courses', courseData);
    return response.data;
  },

  updateCourse: async (courseId, courseData) => {
    const response = await axiosInstance.put(`/courses/${courseId}`, courseData);
    return response.data;
  },

  deleteCourse: async (courseId) => {
    const response = await axiosInstance.delete(`/courses/${courseId}`);
    return response.data;
  },

  // 图书管理
  getBooks: async () => {
    const response = await axiosInstance.get('/books');
    return response.data;
  },

  createBook: async (bookData) => {
    const response = await axiosInstance.post('/books', bookData);
    return response.data;
  },

  updateBook: async (bookId, bookData) => {
    const response = await axiosInstance.put(`/books/${bookId}`, bookData);
    return response.data;
  },

  deleteBook: async (bookId) => {
    const response = await axiosInstance.delete(`/books/${bookId}`);
    return response.data;
  },

  // 作业管理
  getAssignments: async () => {
    const response = await axiosInstance.get('/assignments');
    return response.data;
  },

  createAssignment: async (assignmentData) => {
    const response = await axiosInstance.post('/assignments', assignmentData);
    return response.data;
  },

  updateAssignment: async (assignmentId, assignmentData) => {
    const response = await axiosInstance.put(`/assignments/${assignmentId}`, assignmentData);
    return response.data;
  },

  deleteAssignment: async (assignmentId) => {
    const response = await axiosInstance.delete(`/assignments/${assignmentId}`);
    return response.data;
  },

  // 讨论管理
  getDiscussions: async () => {
    const response = await axiosInstance.get('/discussions');
    return response.data;
  },

  createDiscussion: async (discussionData) => {
    const response = await axiosInstance.post('/discussions', {
      ...discussionData,
      createdAt: new Date().toISOString(),
      likes: 0,
      comments: []
    });
    return response.data;
  },

  updateDiscussion: async (discussionId, discussionData) => {
    const response = await axiosInstance.put(`/discussions/${discussionId}`, discussionData);
    return response.data;
  },

  deleteDiscussion: async (discussionId) => {
    const response = await axiosInstance.delete(`/discussions/${discussionId}`);
    return response.data;
  },

  updateDiscussionStatus: async (discussionId, status) => {
    const response = await axiosInstance.patch(`/discussions/${discussionId}`, { status });
    return response.data;
  }
};

export default adminApi;